station_cfg={}
station_cfg.ssid="useradmin"
station_cfg.pwd="qinjingaoq"

station_cfg.save=true
wifi.sta.config(station_cfg)
wifi.sta.connect()
timer=tmr.create()
timer2=tmr.create()
pin = 5

function dhtRead()
    status, temp, humi, temp_dec, humi_dec = dht.read(pin)
    if status == dht.OK then
        -- Integer firmware using this example
        print(string.format("DHT Temperature:%d.%03d;Humidity:%d.%03d\r\n",
              math.floor(temp),
              temp_dec,
              math.floor(humi),
              humi_dec
        ))
        -- Float firmware using this example
        -- print("DHT Temperature:"..temp..";".."Humidity:"..humi)
    
    elseif status == dht.ERROR_CHECKSUM then
        print( "DHT Checksum error." )
    elseif status == dht.ERROR_TIMEOUT then
        print( "DHT timed out." )
    end
	return temp,humi
end


function reconnect_wifi()
    if wifi.sta.getip() == nil then
        print('Waiting for IP ...')
    else
        timer:stop()

        timer2:alarm(60000, tmr.ALARM_AUTO, send_temp) 
    end
end

function send_temp()
    if wifi.sta.getip() == nil then
		timer:start()
        print('Waiting for IP ...')
    else
        timer:stop()
		temp,humidity=dhtRead()
        http.get("http://192.168.1.5:8080/addData.php?t="..temp.."&h="..humidity, nil, function(code, data)
            if (code < 0) then
              print("HTTP request failed")
            else
              print(code, data)
            end
          end)
        
    end
end

timer:alarm(1000, tmr.ALARM_AUTO, reconnect_wifi) 
